package com.heima.article.listen;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.message.NewsUpOrDownConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class ArticleUpOrDownListener {
    @Autowired
    private ApArticleConfigService apArticleConfigService;

    /**监听器  监听下架消息
     *
     */
    @RabbitListener(queuesToDeclare = @Queue(value = NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE))
    public void newsDownHandle(String articleID){
        log.info("收到自媒体文章下架消息  准备修改文章状态 文章id为:{}",articleID);
        try{
            //修改文章状态
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .set(ApArticleConfig::getIsDown,false)
                    .eq(ApArticleConfig::getArticleId,articleID));
        }catch (Exception e){
            e.printStackTrace();
            log.error("文章下架失败 文章id:{}  原因:{}",articleID,e.getMessage());
        }
    }

    /**监听器  监听下架消息
     *
     */
    @RabbitListener(queuesToDeclare = @Queue(value = NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE))
    public void newsUpHandle(String articleID){
        log.info("收到自媒体文章上架消息  准备修改文章状态 文章id为:{}",articleID);
        try{
            //修改文章状态
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .set(ApArticleConfig::getIsDown,true)
                    .eq(ApArticleConfig::getArticleId,articleID));
        }catch (Exception e){
            e.printStackTrace();
            log.error("文章上架失败 文章id:{}  原因:{}",articleID,e.getMessage());
        }
    }

}
